package com.wx.spider.rocketmq.consumer;

import com.wx.spider.rocketmq.annotation.WsRocketMQConsumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

/**
 * @author:feng
 * @create:2021-04-21 12:21
 * 消费者抽象类
 */
@Slf4j
public abstract class BaseRocketMQConsumer {
    @Value("${rocketmq.name-server}")
    private String namesrvAddr;
    @Value("${rocketmq.consumer.group}")
    private String groupName;

    @PostConstruct
    public void init() {

        WsRocketMQConsumer annotation = this.getClass().getAnnotation(WsRocketMQConsumer.class);
        String nameServer = annotation.nameServer();
        if(nameServer != null && !"".equals(nameServer)){
            namesrvAddr = nameServer;
        }
        String consumerGroup = annotation.consumerGroup();
        if(consumerGroup != null && !"".equals(consumerGroup)){
            groupName = consumerGroup;
        }

        String topicStr = annotation.topic();

        String selectorExpression = annotation.selectorExpression();

        int consumeThreadMin = annotation.consumeThreadMin();
        int consumeThreadMax = annotation.consumeThreadMax();
        MessageModel messageModel = annotation.messageModel();

        int consumeMessageBatchMaxSize = annotation.consumeMessageBatchMaxSize();

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.setConsumeThreadMin(consumeThreadMin);
        consumer.setConsumeThreadMax(consumeThreadMax);
        consumer.setMessageModel(messageModel);
        try {
            consumer.subscribe(topicStr, selectorExpression);
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
            consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeOrderlyContext) -> {
                try {
                    for (MessageExt messageExt : list) {
                        String tag = messageExt.getTags();
                        String topic = messageExt.getTopic();
                        return consume(tag, topic, messageExt);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //稍后再试
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                //消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            });
            consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize);
            consumer.start();
            System.out.println("消费者启动完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected abstract ConsumeConcurrentlyStatus consume(String tag, String topic, MessageExt messageExt);

}